package ca.etsmtl.log430.common;

/**
 * Class in the management layer for querying the resources' data through the actual resource data reader.
 * Plays a role of interface between the Presentation and Data layer
 * @author Adam
 *
 */
public class ResourceAccessManager {
       
        /**
         * The list of drivers.
         */
        private ResourceList listOfResources = new ResourceList();
       
        /**
         * Default
         */
        public ResourceAccessManager() {

                listOfResources = null;
        }
       
        /**
         *
         * @param resourceFile
         */
        public ResourceAccessManager(String resourceFile)
        {
                ResourceReader resourceReader = new ResourceReader(resourceFile);
                generateResourceList(resourceReader.getListOfResources());
        }
       
        /**
         *
         * @param listOfResourceDatas
         */
        private void generateResourceList(List listOfResourceDatas)
        {
                ResourceData resourceData;
                Resource resource;
                listOfResourceDatas.goToFrontOfList();
               
                while ((resourceData = (ResourceData)listOfResourceDatas.getItemFromList()) != null)
                {
                        resource = new Resource();
                        resource.setID(resourceData.getId());
                        resource.setFirstName(resourceData.getFirstname());
                        resource.setLastName(resourceData.getLastname());
                        resource.setRole(resourceData.getRole());
                       
                        String projectIds[] = resourceData.getProjectIds().split("_");
                        for (int i = 0; i < projectIds.length; ++i)
                        {
                                if (!projectIds[i].equals(""))
                                        resource.getPreviouslyAssignedProjectList().addProject(new Project(projectIds[i]));
                        }
                       
                        // Add the resource to the ResourceList
                        listOfResources.addResource(resource);
                }
        }
       
        /**
         *
         * @return
         */
        public ResourceList getListOfResources()
        {
                return this.listOfResources;
        }
}

